package com.jule.miaozhuapc.listener;

import com.jule.miaozhuapc.domain.WawaRoom;
import com.jule.miaozhuapc.service.RoomService;
import com.jule.miaozhuapc.webSocket.WawaS1Socket.S1SocketUser;
import com.jule.miaozhuapc.webSocket.WawaS1Socket.S1ThreadReader;
import com.jule.miaozhuapc.webSocket.control.WawaControlWebSocket;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import java.net.Socket;
import java.util.List;


/**
 * Application Lifecycle Listener implementation class ScoketStart
 *
 */
@WebListener
public class WebSocketStartListenter implements ServletContextListener {


	private static Logger logger = Logger.getLogger(WebSocketStartListenter.class);

	@Autowired
	RoomService roomService;
	/**
	 * Default constructor.
	 */
	public WebSocketStartListenter() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see ServletContextListener#contextInitialized(ServletContextEvent)
	 */
	public void contextInitialized(ServletContextEvent arg0) {
		// 注入 roomService
		if(roomService == null){
			WebApplicationContextUtils.getRequiredWebApplicationContext(arg0.getServletContext()).getAutowireCapableBeanFactory().autowireBean(this);
		}
		List<WawaRoom> rooms = roomService.findAllRoom();
		if (rooms != null && rooms.size() > 0)
		for (WawaRoom room : rooms) {

//			final Integer wawaServerIp = new Long(room.getId()).intValue();
//			final Integer socketIp = Integer.parseInt(room.getScoket());
//			logger.info("socketIp =" +socketIp +" socketServerIp" + wawaServerIp);
//			 //启动游戏主板
//			new Thread(new Runnable() {
//				@Override
//				public void run() {
//				// 启动主板端口
//				try {
//					ServerSocket ss = new ServerSocket(socketIp);
//					Socket s = null;
//					logger.info("主板 accepting..."+ss.getLocalPort());
//					//new Thread(new Test()).start();
//					while ((s = ss.accept()) != null) {
//						logger.info("主板 "+socketIp+" 连接过来了"+ss.getLocalPort());
//						// 加主板
//						WawaServer.socketRooms.put(wawaServerIp, s);
//						// 房间标识为可用
//						WawaServerGameOverCallback.socketRoomsLock.put(wawaServerIp, false);
//						//每个主板开启一个线程
//						new WawaServer.ReceiveThread(s, wawaServerIp).start();
//					}
//					ss.close();
//				} catch (IOException e) {
//					e.printStackTrace();
//				}
//				}
//			}).start();

			// 模拟 用户与wawa服务器1进行通信
			final Long wawaS1UserId = room.getS1UserId();
			final Integer roomId = room.getId().intValue();
			new Thread(new Runnable() {
				@Override
				public void run() {
					try { //likewawa.com
						Socket socket = new Socket("likewawa.com",10201);
						//对象
						S1SocketUser s1SocketUser = new S1SocketUser();
						s1SocketUser.setS1UserId(wawaS1UserId);
						s1SocketUser.setSocket(socket);
						WawaControlWebSocket.RoomSocketUser.put(roomId, s1SocketUser);

						// 建立输入流连接
						new S1ThreadReader(socket, roomId).start();
					}catch (Exception e){
						e.printStackTrace();
					}
				 }
			}).start();
		}


	}

	/**
	 * @see ServletContextListener#contextDestroyed(ServletContextEvent)
	 */
	public void contextDestroyed(ServletContextEvent arg0) {
		// TODO Auto-generated method stub

	}

}
